﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Utility;
using System.Collections.Generic;
using Telerik.WebControls;

namespace Runda.CMS.Portal.SyncManage
{
    public partial class AccessoryInfo : BasePage
    {
        public Guid AccessoryID
        {
            get
            {
                if (hiddenAccesseryID.Value == string.Empty) return Guid.Empty;
                else return new Guid(hiddenAccesseryID.Value);
            }
            set
            {
                hiddenAccesseryID.Value = value.ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlVendor.BindDDDW(new CommonBll().dddw("vendor"), "vendor", "vendor");
                if (Request.QueryString["accessoryID"] != null)
                {
                    WsmAccessoryInfoEntity entity = new SystemBll().Get<WsmAccessoryInfoEntity>(new Guid(Request.QueryString["accessoryID"]));
                    if (entity != null)
                    {
                        AccessoryID = entity.AccessoryID;
                        CompanyInstrumentCode.Text = entity.CompanyInstrumentCode;
                        FactoryInstrumentCode.Text = entity.FactoryInstrumentCode;
                        AccessoryName.Text = entity.AccessoryName;
                        AccessoryStandard.Text = entity.AccessoryStandard;
                        unit.Text = entity.Unit;
                        ddlVendor.SelectedValue = entity.Vendor;
                        PresellPrice.Text = entity.PresellPrice.ToString();
                        LowStockQuantity.Text = entity.LowStockQuantity.ToString();
                        rbAccessoryType.SelectedValue = entity.AccessoryType.ToString();
                    }
                }
            }
        }

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            WsmAccessoryInfoEntity entity = new WsmAccessoryInfoEntity();
            if (AccessoryID != Guid.Empty)
            {
                entity = new SystemBll().Get<WsmAccessoryInfoEntity>(AccessoryID);
            }
            else
            {
                entity.CreatedOn = DateTime.Now;
                entity.Deleted = false;
                entity.AccessoryID = Guid.NewGuid();
                AccessoryID = entity.AccessoryID;
            }

            entity.CompanyInstrumentCode = CompanyInstrumentCode.Text.Trim();
            entity.FactoryInstrumentCode = FactoryInstrumentCode.Text.Trim();
            entity.AccessoryName = AccessoryName.Text.Trim();
            entity.AccessoryStandard = AccessoryStandard.Text.Trim();
            entity.Unit = unit.Text.Trim();
            entity.Vendor = ddlVendor.SelectedValue.ToString();
            entity.AccessoryType = System.Convert.ToInt32(rbAccessoryType.SelectedValue);
            if (PresellPrice.Value != null)
                entity.PresellPrice = System.Convert.ToDecimal(PresellPrice.Value);
            if (LowStockQuantity.Value != null)
                entity.LowStockQuantity = System.Convert.ToDecimal(LowStockQuantity.Value);
            new SystemBll().InsertOrUpdate(entity);
            ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功');window.opener=null;window.open('','_self');window.close();", true);
        }
    }
}
